﻿using System.IdentityModel.Claims;
using System.Security.Principal;

namespace IdentityAtRest.Utility.Identity
{
    public class ClaimsPrincipal : IClaimsPrincipal
    {
        private readonly IClaimsIdentity _identity;

        public ClaimsPrincipal(IClaimsIdentity identity)
        {
            _identity = identity;
        }

        #region IClaimsPrincipal Members

        public bool IsInRole(string role)
        {
            if (ClaimsIdentity.Claims.ClaimEquals(CustomClaimTypes.SuperUser, true))
                return true;

            return ClaimsIdentity.Claims.ClaimEquals(CustomClaimTypes.Role, role);
        }

        public IIdentity Identity
        {
            get { return _identity; }
        }

        public IClaimsIdentity ClaimsIdentity
        {
            get { return _identity; }
        }

        public T GetClaimValue<T>(string key)
        {
            if (ClaimsIdentity == null)
                return default(T);
            return ClaimsIdentity.Claims.GetClaimValue<T>(key);
        }

        #endregion
    }
}